namespace CommontLibrary
{
    using System;
    using System.Data;
    using System.Configuration;   

    /// <summary>
    /// Summary description for DateTimeUntil
    /// </summary>
    public class DateTimeUntil
    {
        public DateTimeUntil()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static DataTable ConvertTableFull(DataTable tblSource, bool blnGetTime)
        {
            string strColumnNames;
            int intIndex;
            int intColIndex;
            string strColumns;
            //string strTemp;
            string strValDate;
            string[] arrColDel;
            strColumnNames = "";
            if ((tblSource != null) && (tblSource.Rows.Count > 0))
            {
                strColumns = "";
                for (intColIndex = 0; intColIndex < tblSource.Columns.Count - 1; intColIndex++)
                {
                    if (System.Type.GetType("System.DateTime").Equals(tblSource.Columns[intColIndex].DataType))
                    {
                        strColumnNames = "";
                        strColumnNames = tblSource.Columns[intColIndex].ColumnName;
                        strColumns = strColumns + strColumnNames + ",";
                        tblSource.Columns.Add(strColumnNames + "_Tmp", Type.GetType("System.String"));
                        for (intIndex = 0; intIndex < tblSource.Rows.Count; intIndex++)
                        {
                            strValDate = "";
                            if (tblSource.Rows[intIndex][strColumnNames] != null)
                            {
                                strValDate = ConvertDate(Convert.ToDateTime(tblSource.Rows[intIndex][strColumnNames].ToString()), blnGetTime) + Convert.ToDateTime(tblSource.Rows[intIndex][strColumnNames].ToString()).ToLongTimeString();
                            }
                            tblSource.Rows[intIndex][strColumnNames + "_Tmp"] = strValDate;
                        }
                    }

                }

                //Dim arrColDel;
                if (strColumns != "")
                {
                    arrColDel = strColumns.Split(',');
                    for (intIndex = arrColDel.GetLowerBound(0); intIndex < arrColDel.GetUpperBound(0); intIndex++)
                    {
                        tblSource.Columns[arrColDel[intIndex]].ColumnName = arrColDel[intIndex] + "_";
                        tblSource.Columns[arrColDel[intIndex] + "_Tmp"].ColumnName = arrColDel[intIndex];
                    }
                }
            }

            return tblSource;
        }
        public static string ConvertDateVNFull(string strDate)
        {
            //' Format DD/MM/YYYY
            DateTime dtmCurrentDate = Convert.ToDateTime(strDate);
            return dtmCurrentDate.Day.ToString().PadLeft(2, '0')
                + "/" + dtmCurrentDate.Month.ToString().Trim().PadLeft(2, '0')
                + "/" + dtmCurrentDate.Year.ToString()
                + "&nbsp;&nbsp;"
                + dtmCurrentDate.Hour.ToString()
                + ":" + dtmCurrentDate.Minute.ToString()
                + ":" + dtmCurrentDate.Second.ToString();
        }
        public static string ConvertDateVNFullBreak(string strDate)
        {
            //' Format DD/MM/YYYY
            DateTime dtmCurrentDate = Convert.ToDateTime(strDate);
            return dtmCurrentDate.Day.ToString().PadLeft(2, '0')
                + "/" + dtmCurrentDate.Month.ToString().Trim().PadLeft(2, '0')
                + "/" + dtmCurrentDate.Year.ToString()
                + "<BR>"
                + dtmCurrentDate.Hour.ToString()
                + ":" + dtmCurrentDate.Minute.ToString()
                + ":" + dtmCurrentDate.Second.ToString();
        }

        public static DateTime ConvertDateCulture(DateTime dtmCurrentDate, string strCulture)
        {
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(strCulture);
            return Convert.ToDateTime(dtmCurrentDate, culture);
        }
        public static DateTime ConvertDateCulture(string strCurrentDate, string strCulture)
        {
            //System.Globalization.CultureInfo[] CultureInfos = System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures);
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(strCulture);
            return Convert.ToDateTime(strCurrentDate, culture);
        }

        public static string ConvertDate(DateTime dtmCurrentDate, bool blnGetE)
        {
            if (blnGetE)
            {
                return dtmCurrentDate.Month.ToString().PadLeft(2, '0')
                    + "/" + dtmCurrentDate.Day.ToString().Trim().PadLeft(2, '0')
                    + "/" + dtmCurrentDate.Year.ToString();
            }
            else
            {

                return dtmCurrentDate.Day.ToString().Trim().PadLeft(2, '0')
                    + "/" + dtmCurrentDate.Month.ToString().PadLeft(2, '0')
                    + "/" + dtmCurrentDate.Year.ToString();
            }

        }

        public static DataTable ConvertTableCulture(DataTable tblSource, string strCulture)
        {
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(strCulture);
            string strColumnNames;
            int intIndex;
            int intColIndex;
            string strColumns;
            //string strTemp;
            string strValDate;
            string[] arrColDel;
            strColumnNames = "";
            if ((tblSource != null) && (tblSource.Rows.Count > 0))
            {
                strColumns = "";
                for (intColIndex = 0; intColIndex < tblSource.Columns.Count - 1; intColIndex++)
                {
                    if (System.Type.GetType("System.DateTime").Equals(tblSource.Columns[intColIndex].DataType))
                    {
                        strColumnNames = "";
                        strColumnNames = tblSource.Columns[intColIndex].ColumnName;
                        strColumns = strColumns + strColumnNames + ",";
                        tblSource.Columns.Add(strColumnNames + "_Tmp", Type.GetType("System.String"));
                        for (intIndex = 0; intIndex < tblSource.Rows.Count; intIndex++)
                        {
                            strValDate = "";
                            if (tblSource.Rows[intIndex][strColumnNames] != null)
                            {
                                strValDate = Convert.ToDateTime(tblSource.Rows[intIndex][strColumnNames].ToString(), culture).ToShortDateString();
                            }
                            tblSource.Rows[intIndex][strColumnNames + "_Tmp"] = strValDate;
                        }
                    }

                }

                //Dim arrColDel;
                if (strColumns != "")
                {
                    arrColDel = strColumns.Split(',');
                    for (intIndex = arrColDel.GetLowerBound(0); intIndex < arrColDel.GetUpperBound(0); intIndex++)
                    {
                        tblSource.Columns[arrColDel[intIndex]].ColumnName = arrColDel[intIndex] + "_";
                        tblSource.Columns[arrColDel[intIndex] + "_Tmp"].ColumnName = arrColDel[intIndex];
                    }
                }
            }

            return tblSource;
        }

        public static DataTable ConvertTable(DataTable tblSource, bool blnGetTime)
        {
            string strColumnNames;
            int intIndex;
            int intColIndex;
            string strColumns;
            //string strTemp;
            string strValDate;
            string[] arrColDel;
            strColumnNames = "";
            if ((tblSource != null) && (tblSource.Rows.Count > 0))
            {
                strColumns = "";
                for (intColIndex = 0; intColIndex < tblSource.Columns.Count - 1; intColIndex++)
                {
                    if (System.Type.GetType("System.DateTime").Equals(tblSource.Columns[intColIndex].DataType))
                    {
                        strColumnNames = "";
                        strColumnNames = tblSource.Columns[intColIndex].ColumnName;
                        strColumns = strColumns + strColumnNames + ",";
                        tblSource.Columns.Add(strColumnNames + "_Tmp", Type.GetType("System.String"));
                        for (intIndex = 0; intIndex < tblSource.Rows.Count; intIndex++)
                        {
                            strValDate = "";
                            if (tblSource.Rows[intIndex][strColumnNames] != null && !tblSource.Rows[intIndex][strColumnNames].ToString().Trim().Equals(""))
                            {
                                strValDate = ConvertDate(Convert.ToDateTime(tblSource.Rows[intIndex][strColumnNames].ToString()), blnGetTime);
                            }
                            tblSource.Rows[intIndex][strColumnNames + "_Tmp"] = strValDate;
                        }
                    }

                }

                //Dim arrColDel;
                if (strColumns != "")
                {
                    arrColDel = strColumns.Split(',');
                    for (intIndex = arrColDel.GetLowerBound(0); intIndex < arrColDel.GetUpperBound(0); intIndex++)
                    {
                        tblSource.Columns[arrColDel[intIndex]].ColumnName = arrColDel[intIndex] + "_";
                        tblSource.Columns[arrColDel[intIndex] + "_Tmp"].ColumnName = arrColDel[intIndex];
                    }
                }
            }

            return tblSource;
        }

        public static DataTable ConvertTableDateTime(DataTable tblSource)
        {
            string strColumnNames;
            int intIndex;
            int intColIndex;
            string strColumns;
            //string strTemp;
            string strValDate;
            string[] arrColDel;
            strColumnNames = "";
            if ((tblSource != null) && (tblSource.Rows.Count > 0))
            {
                strColumns = "";
                for (intColIndex = 0; intColIndex < tblSource.Columns.Count - 1; intColIndex++)
                {
                    if (System.Type.GetType("System.DateTime").Equals(tblSource.Columns[intColIndex].DataType))
                    {
                        strColumnNames = "";
                        strColumnNames = tblSource.Columns[intColIndex].ColumnName;
                        strColumns = strColumns + strColumnNames + ",";
                        tblSource.Columns.Add(strColumnNames + "_Tmp", Type.GetType("System.String"));
                        for (intIndex = 0; intIndex < tblSource.Rows.Count; intIndex++)
                        {
                            strValDate = "";
                            if (tblSource.Rows[intIndex][strColumnNames] != null)
                            {
                                strValDate = ConvertDateVNFull(tblSource.Rows[intIndex][strColumnNames].ToString());
                            }
                            tblSource.Rows[intIndex][strColumnNames + "_Tmp"] = strValDate;
                        }
                    }

                }

                //Dim arrColDel;
                if (strColumns != "")
                {
                    arrColDel = strColumns.Split(',');
                    for (intIndex = arrColDel.GetLowerBound(0); intIndex < arrColDel.GetUpperBound(0); intIndex++)
                    {
                        tblSource.Columns[arrColDel[intIndex]].ColumnName = arrColDel[intIndex] + "_";
                        tblSource.Columns[arrColDel[intIndex] + "_Tmp"].ColumnName = arrColDel[intIndex];
                    }
                }
            }

            return tblSource;
        }
    }
}